package itstudio.instructor.adapter;

import itstudio.app.wq.R;
import itstudio.instructor.entity.News;
import itstudio.instructor.util.TimeUtil;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class NewsListAdapterNoHead extends BaseAdapter {
	private class ViewHolder {
		public TextView txt_title;
		public TextView txt_date;
		public TextView txt_short_content;
	}

	// 单行的布局
	private int mResource;
	private Context context;
	private LayoutInflater mInflater;
	private List<News> newsList =new ArrayList<News>();
	private News news ;
	

	/**
	 * @param context
	 * @param commentList
	 * @param resource
	 * @param editText
	 */
	public NewsListAdapterNoHead(Context context, List<News> newsList, int resource) {

		this.context = context;
		mResource = resource;
		this.newsList = newsList;
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		if(newsList==null){
			return 0;
		}
		return newsList.size();
	}

	@Override
	public Object getItem(int position) {
		
		return newsList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	public void appendToList(List<News> lists) {

		if (lists == null) {
			return;
		}
		if(newsList==null){
			newsList =new ArrayList<News>();
		}
		newsList.addAll(lists);
		notifyDataSetChanged();
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			// 使用自定义的list_items作为layout
			convertView = LayoutInflater.from(context).inflate(mResource,
					parent, false);
			holder = new ViewHolder();
			holder.txt_title = (TextView) convertView.findViewById(R.id.list_item_title);
			holder.txt_short_content = (TextView) convertView.findViewById(R.id.list_item_content);
			holder.txt_date = (TextView) convertView.findViewById(R.id.list_item_time);
			// 设置标记
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		// 设置数据到view
		News news  = newsList.get(position);
		holder.txt_title.setText(news.getTitle());
		holder.txt_short_content.setText(news.getContent());
		holder.txt_date.setText(TimeUtil.dateToString(news.getPostTime(),TimeUtil.FORMAT_DATE));

		return convertView;
	}

}
